package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Equapply;
import com.mbyte.easy.admin.service.IEquapplyService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/equapply/equapply")
public class EquapplyController extends BaseController  {

    private String prefix = "equapply/equapply/";

    @Autowired
    private IEquapplyService equapplyService;

    @Autowired
    SysUserMapper sysUserMapper;


    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param equapply
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String applyCreatetimeSpace, String applyUpdatetimeSpace,
                        Equapply equapply) {
        Page<Equapply> page = new Page<Equapply>(pageNo, pageSize);
        QueryWrapper<Equapply> queryWrapper = new QueryWrapper<Equapply>();
        if(!ObjectUtils.isEmpty(equapply.getApplyId())) {
            queryWrapper = queryWrapper.like("apply_id",equapply.getApplyId());
         }
        if(!ObjectUtils.isEmpty(equapply.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",equapply.getEquId());
         }
        if(!ObjectUtils.isEmpty(equapply.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",equapply.getEquName());
         }
        if(!ObjectUtils.isEmpty(equapply.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",equapply.getEquType());
         }
        if(!ObjectUtils.isEmpty(equapply.getApplyPersonid())) {
            queryWrapper = queryWrapper.like("apply_personId",equapply.getApplyPersonid());
         }
        if(!ObjectUtils.isEmpty(equapply.getApplyStatus())) {
            queryWrapper = queryWrapper.like("apply_status",equapply.getApplyStatus());
         }
        if(!ObjectUtils.isEmpty(equapply.getApplyReason())) {
            queryWrapper = queryWrapper.like("apply_reason",equapply.getApplyReason());
         }
        if(!ObjectUtils.isEmpty(equapply.getApplyCreatetime())) {
            queryWrapper = queryWrapper.like("apply_createtime",equapply.getApplyCreatetime());
         }
        if(!ObjectUtils.isEmpty(equapply.getApplyUpdatetime())) {
            queryWrapper = queryWrapper.like("apply_updatetime",equapply.getApplyUpdatetime());
         }
        if(!ObjectUtils.isEmpty(equapply.getApplyRemake())) {
            queryWrapper = queryWrapper.like("apply_remake",equapply.getApplyRemake());
         }

        String  sysUser = Utility.getCurrentUsername();
        SysUser account=sysUserMapper.selectByUsername(sysUser);
        model.addAttribute("account", account.getAccount());

        IPage<Equapply> pageInfo = equapplyService.page(page, queryWrapper);
        model.addAttribute("applyCreatetimeSpace", applyCreatetimeSpace);
        model.addAttribute("applyUpdatetimeSpace", applyUpdatetimeSpace);
        model.addAttribute("searchInfo", equapply);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param equapply
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Equapply equapply){
        return toAjax(equapplyService.save(equapply));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("equapply",equapplyService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param equapply
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Equapply equapply){
        return toAjax(equapplyService.updateById(equapply));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        Equapply equapply =equapplyService.getById(id);
        if (equapply.getApplyStatus()!=1){
            return toAjax(equapplyService.removeById(id));
        }
        return toAjax(false);
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(equapplyService.removeByIds(ids));
    }





    /**
     *
     */
    @RequestMapping("adminapply")
    public String adminapply(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String applyCreatetimeSpace, String applyUpdatetimeSpace,
                        Equapply equapply) {
        Page<Equapply> page = new Page<Equapply>(pageNo, pageSize);
        QueryWrapper<Equapply> queryWrapper = new QueryWrapper<Equapply>();
        if(!ObjectUtils.isEmpty(equapply.getApplyId())) {
            queryWrapper = queryWrapper.like("apply_id",equapply.getApplyId());
        }
        if(!ObjectUtils.isEmpty(equapply.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",equapply.getEquId());
        }
        if(!ObjectUtils.isEmpty(equapply.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",equapply.getEquName());
        }
        if(!ObjectUtils.isEmpty(equapply.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",equapply.getEquType());
        }
        if(!ObjectUtils.isEmpty(equapply.getApplyPersonid())) {
            queryWrapper = queryWrapper.like("apply_personId",equapply.getApplyPersonid());
        }
        if(!ObjectUtils.isEmpty(equapply.getApplyStatus())) {
            queryWrapper = queryWrapper.like("apply_status",equapply.getApplyStatus());
        }
        if(!ObjectUtils.isEmpty(equapply.getApplyReason())) {
            queryWrapper = queryWrapper.like("apply_reason",equapply.getApplyReason());
        }
        if(!ObjectUtils.isEmpty(equapply.getApplyCreatetime())) {
            queryWrapper = queryWrapper.like("apply_createtime",equapply.getApplyCreatetime());
        }
        if(!ObjectUtils.isEmpty(equapply.getApplyUpdatetime())) {
            queryWrapper = queryWrapper.like("apply_updatetime",equapply.getApplyUpdatetime());
        }
        if(!ObjectUtils.isEmpty(equapply.getApplyRemake())) {
            queryWrapper = queryWrapper.like("apply_remake",equapply.getApplyRemake());
        }

        String  sysUser = Utility.getCurrentUsername();
        SysUser account=sysUserMapper.selectByUsername(sysUser);
        model.addAttribute("account", account.getAccount());

        IPage<Equapply> pageInfo = equapplyService.page(page, queryWrapper);
        model.addAttribute("applyCreatetimeSpace", applyCreatetimeSpace);
        model.addAttribute("applyUpdatetimeSpace", applyUpdatetimeSpace);
        model.addAttribute("searchInfo", equapply);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"admin/adminapply";
    }
}

